package com.tool.grab.vpn;

import java.util.LinkedHashMap;

public class CacheMap<K,V> extends LinkedHashMap<K,V> {
    private int maxLength;
    private DeleteCallback<V> callback;
    public CacheMap(int maxLength,DeleteCallback<V> callback){
        super(maxLength+1,1,true);
        this.maxLength = maxLength;
        this.callback = callback;
    }

    @Override
    protected boolean removeEldestEntry(Entry<K, V> eldest) {
        if(size() > maxLength){
            callback.delete(eldest.getValue());
            return true;
        }
        return false;
    }


    public interface DeleteCallback<V>{
        void delete(V v);
    }
}
